package com.huike.web.controller.review;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.review.pojo.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;
    /**
     * 增 加
     */
    @GetMapping("saveData/{name}/{age}/{sex}")  // 直接将参数传到路径上面时 用@PathVariable
    public  AjaxResult saveData(@PathVariable String name,@PathVariable Integer age, @PathVariable String sex){
        int i = reviewService.saveData(name, age, sex);
        return AjaxResult.success(i);
    }
//    //@GetMapping("/saveData/{name}/{age}/{sex}")
//    public String saveData(@PathVariable String name ,@PathVariable Integer age ,@PathVariable String sex){
//        int i = reviewService.saveData(name, age, sex);
//        return "添加成功";
//    }
    @PostMapping("/saveData1") //异步提交发送，参数通过 '请求体' 传送json数据时用@RequestBody
     public AjaxResult saveData1(@RequestBody Review review){
        int i = reviewService.saveData1(review);
        return AjaxResult.success(i);
    }
//    @PostMapping("/saveData1")
//    public String saveData1(@RequestBody  Review review){
//        int i = reviewService.saveData1(review);
//        return "添加成功";
//    }
    /**=========================================================新增数据============================================*/

    /**=========================================================删除数据=============================================*/
    @DeleteMapping("/remove/{id}") //// 直接将参数传到路径上面时 用@PathVariable
        public AjaxResult delete (@PathVariable Long id){
        int remove = reviewService.remove(id);
        return AjaxResult.success(remove);
    }
//    @DeleteMapping("/remove/{id}")
//    public String delete(@PathVariable Long id){
//        int remove = reviewService.remove(id);
//        return  "删除成功";
//    }
    /**=========================================================修改数据=============================================*/
    @PostMapping("/update") //异步提交发送，参数通过 '请求体' 传送json数据时用@RequestBody
    public AjaxResult  update(@RequestBody Review review){
        int update = reviewService.update(review);
        return AjaxResult.success(update);
    }
//    @PostMapping("update")
//    public String update(@RequestBody Review review){
//        int update = reviewService.update(review);
//        return "修改成功";
//    }
    /**=========================================================查询数据=============================================*/
    //单条件查询  根据id 查询数据
    @GetMapping("/getById")
    public AjaxResult getById(Long id){
        Review byId = reviewService.getById(id);
        return AjaxResult.success(byId);
    }
//   @GetMapping("/getById")
//    public AjaxResult getById(Long id){
//       Review byId = reviewService.getById(id);
//       return AjaxResult.success(byId);
//   }

    //分页查询
    @GetMapping("/getDataByPage")
    public AjaxResult pageData (Integer pageNum,Integer pageSize){
        //分页插件 pom 已 提供
        PageHelper.startPage(pageNum,pageSize);
       // Review review = new Review();
        //调用service 层 的 page 方法
        List<Review> page = reviewService.page();
        //分页器 构造器
        PageInfo<Review> reviewPageInfo = new PageInfo<>(page);
        return AjaxResult.success(reviewPageInfo);

    }

//   @GetMapping("/getDataByPage")
//  public AjaxResult pageData ( Integer pageNum , Integer pageSize){
//       Page<Object> objects = PageHelper.startPage(pageNum, pageSize);
//       Review review = new Review();
//       List<Review> page = reviewService.page(review);
//       PageInfo<Review> page1 = new PageInfo<>(page);
////       List<Review> page = reviewService.page(pageNum, pageSize);
//
//       return AjaxResult.success(page1);
   }

